package controller

import (
	"encoding/gob"
	"fileshare/conf"
	models "fileshare/controller/modles"
	"fileshare/utils"
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	"net/http"
)

/**
* @program: fileshare
*
* @description:
*
* @author: Koco.Allien
*
* @create: 2020-04-05 16:00
**/
func RouterRun() *gin.Engine {
	r := gin.Default()

	//设置
	gin.SetMode(conf.Cfg.Key("runmode").MustString(gin.DebugMode))
	r.LoadHTMLGlob("views/*")
	r.Static("/static", "static")
	//测试路由
	r.GET("/ping", testping)
	r.POST("/post", testpost)

	//中间件
	r.Use(gin.LoggerWithConfig(utils.MyLogconf))
	r.Use(gin.Recovery())
	//session设置
	gob.Register(models.UserInfo{})
	store := cookie.NewStore([]byte("loginuser"))
	r.Use(sessions.Sessions("mysession", store))

	//正式路由
	//主页
	r.GET("/", IndexController)      //主页
	r.GET("/index", IndexController) //主页
	//登陆
	r.GET("/login", LoginController)         //登陆页面
	r.POST("/login", LoginControllerPost)    //登陆页面
	r.GET("/loginout", LoginControllerToOut) //登出
	//注册
	r.GET("/register", RegistController)      //注册
	r.POST("/register", RegistControllerPost) //注册
	//文件操作
	r.GET("/upload", UploadController)      //文件上传 get方法返回上传文件的页面
	r.POST("/upload", UploadControllerPost) //文件上传 上传文件
	r.GET("/download", DownController)      //文件的下载
	//用户列表
	r.GET("/u", UserSelfController)              //用户列表 selfuser.html
	r.GET("/udelete", UserDelController)         //删除用户 uid={{$elem.ID}}&&active={{.active}}
	r.GET("/uupdate", UserUpdateController)      //用户更新页面 uid={{$elem.ID}}&&active={{.active}}
	r.POST("/uupdate", UserUpdateControllerPost) //更新用户
	//个人文件列表
	r.GET("/f", FileSelfController)            //个人文件 selffile.html
	r.GET("/fdelete", FdeleteController)       //删除文件 fid={{$elem.ID}}&&active={{.active}}
	r.GET("/fupdate", FupdateController)       //更新文件名称 fid={{$elem.ID}}&&active={{.active}}
	r.POST("/fupdate", FfupdateControllerPost) //更新文件名称
	//个人分享列表
	r.GET("/sf", ShareSelfController)      //个人分享列表 sharefile.html
	r.GET("/sfdelete", SfdeleteController) //删除分享 fid={{$elem.ID}}&&active={{.active}}&&shareid=1
	//分享的方式
	r.GET("/sfadduf", SfaddufController)      //添加分享 用户--> 文件 shareuf.html
	r.POST("/sfadduf", SfaddufControllerPost) //添加分享
	r.GET("/sfaddfu", SfaddfuController)      //添加分享 文件--> 用户 sharefu.html
	r.POST("/sfaddfu", SfaddfuControllerPost) //添加分享
	//结束路由

	return r
}

//ping
func testping(c *gin.Context) {
	c.JSON(http.StatusOK, "pang")
}

//testpost
/*
POST /post?id=1234&page=1 HTTP/1.1
Content-Type: application/x-www-form-urlencoded

name=manu&message=this_is_great
*/
func testpost(c *gin.Context) {
	id := c.Query("id")
	page := c.DefaultQuery("page", "0")
	name := c.PostForm("name")
	message := c.PostForm("message")
	fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message)
}
